/**
 * 
 */
package jvs.vfs.opt.contact;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import jvs.vfs.IFile;
import jvs.vfs.opt.ItemInfo;
import jvs.vfs.opt.ItemListFileImpl;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public class EmailContactFileImpl extends ItemListFileImpl {

	private static final String[] LIST = { "/all", "/favorite", "/most", "/recent",
			  };
	
	private static ItemInfo[][] lists = new ItemInfo[LIST.length][];

	private static final int LIST_ALL = 0;

	private static final int LIST_FAVE = 1;

	private static final int LIST_MOST = 2; // times called

	private static final int LIST_RECENT = 3; // last time called


	private static final String LOG_TAG = "PhoneContactFileImpl";

	private static Comparator<? super ContactInfo> mostComparator = new Comparator<ContactInfo>() {
		public int compare(ContactInfo c1, ContactInfo c2) {
			//desc
			if (c1 == null) {
				return -1;
			}
			if (c2 == null) {
				return 1;
			}
			int t = (c2.times - c1.times);
			if (t != 0) {
				return t;
			}
			return c1.name.compareTo(c2.name);
		}
	};

	private static Comparator<? super ContactInfo> recentComparator = new Comparator<ContactInfo> () {

		public int compare(ContactInfo c1, ContactInfo c2) {
			//desc
			if (c1 == null) {
				return -1;
			}
			if (c2 == null) {
				return 1;
			}
			long t1 = c1.lastTime;
			long t2 = c2.lastTime;
			if (t1 > t2) {
				return -1;
			}
			if (t1 < t2) {
				return 1;
			}
			//t1 == t2
			return c1.name.compareTo(c2.name);
		}
		
	};

	private static void addSortKey(List<ContactInfo> list) {
		int len = list.size();
		long key = System.currentTimeMillis();
		for (int i = 0; i < len; i++) {
			list.get(i).sortkey = (key + i) + "";
		}
	}

	synchronized private static void loadAll(Context ctx,
			List<ContactInfo> list, final int idx) {
		ContactUtils.loadEmailContact(ctx, list);
	}

	synchronized private static void loadFave(Context ctx,
			List<ContactInfo> list, int idx) {
		ContactUtils.loadEmailFavorite(ctx, list);
	}

	synchronized private static void loadMost(Context ctx,
			List<ContactInfo> list, int idx) {
		ContactUtils.loadEmailContact(ctx, list);
		// sort by times desc
		Collections.sort(list, mostComparator);
		addSortKey(list);
	}

	synchronized private static void loadRecent(Context ctx,
			List<ContactInfo> list, int idx) {
		ContactUtils.loadEmailContact(ctx, list);
		// sort by last time desc
		Collections.sort(list, recentComparator);
		addSortKey(list);
	}

	/**
	 * @param uri
	 */
	public EmailContactFileImpl(URI uri) {
		super(uri);
		//
		if (DEBUG) {
			Log.d(LOG_TAG, "EmailContactFileImpl " + uri);
		}
	}

	protected EmailContactFileImpl(URI uri, ItemInfo info) {
		super(uri, info);
	}

	@Override
	public boolean execute() {
		try {
			final ContactInfo item = (ContactInfo) info;
			if (item == null) {
				return false;
			}
			Context ctx = (Context) fs.getContext();
			Intent i = ContactUtils.createEmailIntent(item.emails);
//			Intent ci = Intent.createChooser(i, item.name);
//			ci.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			ctx.startActivity(ci);
			i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
			ctx.startActivity(i);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public String getDisplayName() {
		ContactInfo item = (ContactInfo) info;
		if (item != null) {
			return item.name;
		}
		return super.getDisplayName();
	}

	@Override
	protected String[] getFolders() {
		return LIST;
	}

	@Override
	public Object getIcon() {
		ContactInfo item = (ContactInfo) info;
		if (item != null) {
			Object obj = item.icon;
			if (obj == null) {
				Context ctx = (Context) fs.getContext();
				Resources res = ctx.getResources();
				return res.getDrawable(android.R.drawable.sym_action_email);
			}
		}
		return super.getIcon();
	}

	@Override
	public long getLastModified() {
		ContactInfo item = (ContactInfo) info;
		if (item != null) {
			return item.lastTime;
		}
		return super.getLastModified();
	}

	@Override
	public String getPhoneticName() {
		ContactInfo item = (ContactInfo) info;
		if (item != null) {
			String pn = item.phoneticName;
			if (TextUtils.isEmpty(pn)) {
				return item.name;
			} else {
				return pn;
			}
		}
		return super.getPhoneticName();
	}

	@Override
	public String getSortKey() {
		ContactInfo item = (ContactInfo) info;
		if (item != null) {
			return item.sortkey;
		}
		return super.getSortKey();
	}

	@Override
	protected ItemInfo[] load(Context ctx, int idx) {
		ArrayList<ContactInfo> list = new ArrayList<ContactInfo>();
		switch (idx) {
		case LIST_ALL:
			loadAll(ctx, list, idx);
			break;
		case LIST_FAVE:
			loadFave(ctx, list, idx);
			break;
		case LIST_RECENT:
			loadRecent(ctx, list, idx);
			break;
		case LIST_MOST:
			loadMost(ctx, list, idx);
			break;
		}
		if (DEBUG) {
			Log.d(LOG_TAG, "EmailContactFileImpl loaded: [" + idx + "] " + list);
		}
		final ItemInfo[] ia = new ItemInfo[0];
		return (list.size() == 0 ? ia : list.toArray(ia));
	}

	@Override
	protected IFile newInstance(URI pu, ItemInfo ai) {
		return new EmailContactFileImpl(pu, ai);
	}

	@Override
	protected ItemInfo[][] lists() {
		return lists;
	}
}
